package com.zy.web.controller.system;

import java.util.*;

import com.zy.common.core.domain.model.LoginUser;
import com.zy.common.utils.StringUtils;
import com.zy.framework.web.service.TokenService;
import com.zy.system.mapper.SysUserMapper;
import com.zy.system.service.ISysPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zy.common.constant.Constants;
import com.zy.common.core.domain.AjaxResult;
import com.zy.common.core.domain.entity.SysMenu;
import com.zy.common.core.domain.entity.SysUser;
import com.zy.common.core.domain.model.LoginBody;
import com.zy.common.utils.SecurityUtils;
import com.zy.framework.web.service.SysLoginService;
import com.zy.framework.web.service.SysPermissionService;
import com.zy.system.service.ISysMenuService;

import javax.annotation.Resource;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@Api(tags = "登录验证")
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysPostService sysPostService;

    @Resource
    private SysUserMapper userMapper;

    @Autowired
    private TokenService tokenService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation("登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @ApiOperation(value = "办公模块登录")
    @RequestMapping(value="/getOfficeByOpenId",method = RequestMethod.GET)
    public AjaxResult officeLogin(@RequestParam(name="openId") String openId){
        SysUser user = userMapper.getUserByOpenId(openId);
        Map<String, Object> map = new HashMap<String, Object>();
        if(user!=null){
            if (StringUtils.isNotEmpty(user.getOpenId()))
                if (!user.getOpenId().equals(openId)) {
                    return AjaxResult.error("此微信账号已绑定其他用户,请联系管理员！");
                }
            user.setOpenId(openId);
            userMapper.updateUser(user);
            LoginUser loginUser = new LoginUser(user.getUserId(), user.getDeptId(), user, permissionService.getMenuPermission(user));
            loginService.recordLoginInfo(loginUser.getUserId());
            // 生成token
            String token = tokenService.createToken(loginUser);
            map.put(Constants.TOKEN, token);
        }else{
            return AjaxResult.error("系统无此用户,请联系管理员！");
        }
        return AjaxResult.success(map);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @ApiOperation("获取用户信息")
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        //
        Set<String> postNames = sysPostService.getPostNames(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("postNames", postNames);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @ApiOperation("获取路由信息")
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
